package com.zwz.controller;
import com.zwz.crawler.annualReports.TungReports;
import com.zwz.crawler.annualReports.TungYearReport;
import com.zwz.crawler.annualReports.TungeeAnnualReportQuery;
import com.zwz.crawler.basic.TungeeBranchOfficeQuery;
import com.zwz.crawler.corpDevelopment.TungeeTaxpayerList;
import com.zwz.crawler.mangeInfo.invertPage.TungeeInvestmentQuery;
import com.zwz.dto.EnterpriseAnnualDTO;
import com.zwz.entity.EnterpriseAnnualReport;
import com.zwz.entity.InvestmentEmploymentInfo;
import com.zwz.resp.annualReports.EnterpriseReport;
import com.zwz.resp.annualReports.Report;
import com.zwz.service.EnterpriseAnnualReportService;
import com.zwz.vo.AccountInfo;
import com.zwz.vo.ReportInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/annual-reports")
public class EnterpriseAnnualReportController {

    @Autowired
    private EnterpriseAnnualReportService service;

    @PostMapping
//    public ResponseEntity<?> create(@RequestBody EnterpriseAnnualReport report) {
    public ResponseEntity<?> create(  String enterpriseId) {
    try {
//            validateReport(report);

        List<ReportInfo> reports = TungYearReport.startTungeeAnnualReportQuery(enterpriseId);
        List<EnterpriseAnnualReport> info = TungeeAnnualReportQuery.startTungeeAnnualReportQuery(enterpriseId);


        int data = TungeeInvestmentQuery.StartTungeeInvestmentQuery(enterpriseId);



        for(int i=0;i<reports.size();i++){
            //1. 从中间层收取
//            info.get(i).setNetProfit(new BigDecimal(reports.get(0).getNetSum()));
            info.get(i).setTotalRevenue( new BigDecimal(reports.get(i).getAboundSum()));
            info.get(i).setTaxAmount(new BigDecimal(reports.get(i).getTaxSum()));
             info.get(i).setTotalProfit(new BigDecimal(reports.get(i).getPureprofit()));
//             info.get(i).setHasReport();

            if(data==0){
                info.get(i).setForeignInvestment("false");
            }else{
                info.get(i).setForeignInvestment("true");
            }
            service.create(info.get(i));
        }


        EnterpriseAnnualDTO dto = new EnterpriseAnnualDTO(null, info);

        //todo 待优化
        return ResponseEntity.status(HttpStatus.CREATED).body(dto);

        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<EnterpriseAnnualReport> getById(@PathVariable Integer id) {
        EnterpriseAnnualReport report = service.getById(id);
        return report != null ? ResponseEntity.ok(report) : ResponseEntity.notFound().build();
    }

    @GetMapping
    public ResponseEntity<List<EnterpriseAnnualReport>> getAll() {
        return ResponseEntity.ok(service.getAll());
    }

    @GetMapping("/year/{year}")
    public ResponseEntity<List<EnterpriseAnnualReport>> getByYear(@PathVariable Integer year) {
        if (year < 1900 || year > 2100) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(service.getByYear(year));
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> update(@PathVariable Integer id, @RequestBody EnterpriseAnnualReport report) {
        try {
            if (!id.equals(report.getId())) {
                return ResponseEntity.badRequest().body("ID不一致");
            }
            validateReport(report);
            service.update(report);
            return ResponseEntity.ok(report);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteById(@PathVariable Integer id) {
        service.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    private void validateReport(EnterpriseAnnualReport report) {
        if (report.getInsuredCount() != null && report.getInsuredCount() < 0) {
            throw new IllegalArgumentException("参保人数不能为负数");
        }
        // 其他校验逻辑...
    }
}